////////////////////////////////////////////////////////////////////////////////////
//
//
//							MiTAC Corporation
//				NO. 187, TIDING BLVD., SEC. 2, TAIPEI, TAIWAN
//
// 			(c) Copyright 2006, MiTAC Corporation, Taipei, Taiwan
//						All rights reserved.
//-------------------------------------------------------------------------------------
//
//
//	File:	timer.c
//	Description:	OMAP850 timer
//
//	Programer: Visual.Wei
//	Date: 	2006-4-13
//
////////////////////////////////////////////
#include <windows.h>
#include <oal.h>
#include <s3c2443.h>
#include <bsp_cfg.h>
#include "kernel_asm.h"
#include "ipl_core.h"

extern UINT32 PWGetPclk();
static volatile UINT32 g_sysTickCount;

static void TimerISR()
{
	static int i=0;
	++g_sysTickCount;
	IPLIntrDoneIrq(IRQ_TIMER4);

	if(i<100)
		i++;
	else
	{
		i=0;
		RETAILMSG (0, (TEXT(">>>1s Timer \r\n")));
	}
	
}

UINT32 IPLGetTickCount()
{
	//10ms
	return g_sysTickCount;
}

void IPLTimerStart()
{
	S3C2443_PWM_REG *PWM;
	DWORD con;
	UINT32 pclk,scaler;

	RETAILMSG (1, (TEXT("+IPLTimerStart\r\n")));
	PWM = (S3C2443_PWM_REG*)OALPAtoVA(S3C2443_BASE_REG_PA_PWM, FALSE);

	//init
	g_sysTickCount=0;
	IPLIntrRequestIrq(IRQ_TIMER4,TimerISR);
	IPLIntrEnableIrq(IRQ_TIMER4);

	pclk=S3C2443_PCLK;
	scaler=((pclk/(100000*16)) - 1);
	//Timer 4 ,clock freq.=100kHz, clock cycle=10us
	CLRREG32(&PWM->TCFG0,0xFF<<8);
	SETREG32(&PWM->TCFG0,scaler<<8);
	//MUX4 is 1/16
	CLRREG32(&PWM->TCFG1,(0xFF<<16)|(0xFF<<20));
	SETREG32(&PWM->TCFG1,(0x3<<16));
	//counter,max 65536,interrupt per 10ms
	OUTREG32(&PWM->TCNTB4 , 1000);
	//config TCON for PWM timer1(update,reload,enable)
	con=INREG32(&PWM->TCON)&(~(0xF<<20));
	OUTREG32(&PWM->TCON,(0x2<<20)|con);
	OUTREG32(&PWM->TCON,(0x5<<20)|con);

	RETAILMSG (1, (TEXT("-IPLTimerStart\r\n")));
}
